<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="24">
          <el-form-item :label="$t('基金名称')" prop="fundName">
            <template>
              <router-link
                target="_blank"
                :to="{path:'/fund/fundBasehome',query:{id:form.fundId}}"
                class="link_a"
              >{{ form.fundName }}
              </router-link>
            </template>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('基金管理人')" prop="fundManager">
            <!--            <el-input v-model="form.fundManager" :placeholder="$t('基金管理人')" show-word-limit maxlength="1000" />-->
            <el-select v-model="form.fundManager" filterable clearable :placeholder="$t('请选择')" disabled>
              <el-option
                v-for="item in gpBaseOptions"
                :key="item.id"
                :label="item.name"
                :value="item.id"
                disabled
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('出资人')" prop="investors">
            <!--            <el-input v-model="form.investors" :placeholder="$t('出资人')" show-word-limit maxlength="32" />-->
            <fund-lp-select
              v-model="form.investors"
              :fund-id="form.fundId"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('本人或家庭成员担任标的公司职位')" prop="companyPosition">
            <el-input
              v-model="form.companyPosition"
              :placeholder="$t('本人或家庭成员担任标的公司职位')"
              show-word-limit
              maxlength="100"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('申请说明')" prop="applicationInstructions">
            <el-input
              v-model="form.applicationInstructions"
              type="textarea"
              :placeholder="$t('申请说明')"
              show-word-limit
              maxlength="1000"
            />
          </el-form-item>
        </el-col>

      </el-row>
      <!-- 附件列表 -->
      <FileTable
        :list.sync="form.fileList"
        data-type="fund_affiliated_party"
        :business-id="form.fundId"
        :btn-show="!otherBtnHide"
      />
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存，保存并启动流程，取消 三个按钮-->
      <FormBtn ref="formBtn" :bus-data="form" has-permi="fund:affiliated" />
    </div>
  </div>
</template>

<script>
import { saveFundAffiliatedParty, getFundAffiliatedParty } from '@/api/fund/affiliated/fundAffiliatedParty'
import { getFundBase } from '@/api/fund/fundBase'
import { findGp } from '@/api/fund/gp/gpBase'
import { getFundInvestor } from '@/api/fund/investor/fundInvestor'
import FundLpSelect from '@/components/Select/fundLpSelect'

export default {
  components: {
    FundLpSelect
  },
  data() {
    return {
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        fundName: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ],
        fundManager: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ],
        investors: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ],
        companyPosition: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ]
      },
      // 遮罩层
      loading: false,
      // 操作按钮默认不展示
      buttonShow: true,
      // 是否隐藏保存按钮
      otherBtnHide: false,
      // GP管理人
      gpBaseOptions: [],
      // 使用流程的key
      processKey: undefined
    }
  },
  created() {
    this.$bus.$emit('async_done')
    // 查询基金管理人
    this.findGpOption()
  },
  methods: {
    // 初始化数据
    init(data, processKey) {
      // 使用流程
      this.processKey = processKey
      this.loading = true
      if (data != null && data.id !== undefined) {
        getFundAffiliatedParty(data.id).then(response => {
          this.form = response.data
          this.loading = false
        }).catch(() => {
          this.loading = false
        })
      } else {
        this.reset()
        // 获取基金详细
        getFundBase(this.form.fundId).then(response => {
          this.form.fundName = response.data.fundFullName
          this.form.fundManager = response.data.fundBaseFund.managerId
          this.loading = false
        }).catch(() => {
          this.loading = false
        })
      }
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        fundId: this.$route.query.id,
        fundName: undefined,
        fundManager: undefined,
        investors: undefined,
        companyPosition: undefined,
        applicationInstructions: undefined,
        actId: undefined,
        actStatus: undefined,
        actTitle: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        delFlag: undefined
      }
      this.resetForm('form')
    },
    // 取消按钮
    cancel() {
      this.$parent.$parent.formOpen = false
      this.$parent.$parent.reset()
    },
    // 关闭form页面
    closeForm() {
      // 关闭form页面遮罩层
      this.loading = false
      // 关闭form页面
      if (this.$parent.$parent.formOpen !== undefined) {
        this.$parent.$parent.formOpen = false
      }
      // 刷新list页面
      if (this.$parent.$parent.getList !== undefined) {
        this.$parent.$parent.getList()
      }
    },
    /** 提交按钮 */
    submitForm(type) {
      return new Promise((resolve, reject) => {
        this.$refs['form'].validate(valid => {
          // 验证数据
          if (valid) {
            // 弹出遮罩层
            this.loading = true
            // 保存数据
            saveFundAffiliatedParty(this.form).then(response => {
              if (response.code === 200) {
                // 点击的按钮是保存并启动流程
                if (type === 'process') {
                  // 启动流程
                  this.startProcess(response.data.id)
                  // 点击的仅保存
                } else {
                  this.msgSuccess(this.$t('保存成功'))
                  // 关闭form页面
                  this.closeForm()
                }
                resolve()
              } else {
                this.msgError(response.msg)
                this.loading = false
                reject()
              }
            })
          } else {
            reject()
            this.locationError(document)
          }
        })
      }).catch(() => {
        this.loading = false
      })
    },
    // 启动流程
    startProcess(dataId) {
      this.loading = false
      // 启动流程时候传入的参数
      const processData = {}
      // 待办标题
      processData.title = this.form.fundName + '-关联方特批'
      // 业务表名
      processData.dataTable = 'fund_affiliated_party'
      // 业务ID
      processData.dataId = dataId
      // 业务表单地址(待办页面需要打开的页面)
      processData.formPath = '@/views/fund/affiliated/fundAffiliatedPartyForm'
      // 使用流程的key
      processData.processKey = this.processKey
      // 菜单标识(根据菜单标识判断的走哪个流程)
      // processData.menuKey = this.$t('菜单标识')
      this.startProcessByMenu(processData, () => {
        this.form.actStatus = 'D'
        this.msgSuccess(this.$t('数据保存成功,流程启动成功'))
        this.closeForm()
      })
    },
    findGpOption() {
      findGp().then(response => {
        this.gpBaseOptions = response.data
        console.log(this.gpBaseOptions)
      })
    },
    async changeCZR() {
      this.$nextTick(() => {
        const investors = this.form.investors
        this.isShowCallInfo = false
        // this.form.payMoney = undefined
        this.form.callMoney = undefined
        if (investors !== '' && investors !== undefined && investors !== null) {
          getFundInvestor(investors).then(response => {
            this.form.subscriptionAmount = response.data.subscribedAmount
            this.fundLPPayloading = false
          })
        }
      })
    }

  }

}
</script>
